//
//  UIViewController+Convenience.swift
//  HuaYun
//
//  Created by enghou on 2018/6/27.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

extension UIViewController{
    
    func alert(_ message: String?) -> Void {
        SVProgressHUD.showInfo(withStatus: message)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(1)) {
            SVProgressHUD.dismiss()
        }
    }
    
    func alert(_ message: String?,cancelTitle: String,cancelBlock:(()->Void)?,sureTitle: String,sureBlock:(()->Void)?) -> Void {
        
        let alert = UIAlertController.init(title: nil, message: message, preferredStyle: .alert)
        
        let sure = UIAlertAction.init(title: sureTitle, style: .default) { (alert) in
            sureBlock?()
        }
        
        alert.addAction(sure)
        
        let cancel = UIAlertAction.init(title: cancelTitle, style: .cancel) { (alert) in
            cancelBlock?()
        }
        
        alert.addAction(cancel)
        
        self.present(alert, animated: true, completion: nil)
        
    }
    
    
    func showError(_ message: String?) -> Void {
        SVProgressHUD.showError(withStatus: message)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(1)) {
            SVProgressHUD.dismiss()
        }
    }
    
    func showSuccess(_ message: String) -> Void {
        SVProgressHUD.showSuccess(withStatus: message)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(1)) {
            SVProgressHUD.dismiss()
        }
    }
    
}

extension UIViewController{
    
    func beginLoading() -> Void {
        SVProgressHUD.show()
    }
    
    func endLoading() -> Void {
        SVProgressHUD.dismiss()
    }
}


extension UIViewController{
    
    func push(destination vc: UIViewController) -> Void {
        
        for view in self.navigationController?.navigationBar.subviews ?? [] {
            if view.tag == 10001 {
               view.isHidden = true
            }
        }
        
        self.view.endEditing(true)
        
        vc.hidesBottomBarWhenPushed = true
        
        if self.navigationController != nil {
            
            if let view = self.navigationController?.navigationBar.viewWithTag(10001) {
                view.isHidden = true
            }
            
            self.navigationController?.pushViewController(vc, animated: true)
            
            return
        }
        
        
        
        
        if let window = UIApplication.shared.delegate?.window {
            
            if let tab = window?.rootViewController as? UITabBarController {
                if let navi = tab.selectedViewController as? UINavigationController {
                    navi.pushViewController(vc, animated: true)
                }
            }
        }
    }
    
    func recovernavigationbar()->Void{
//        self.navigationController?.navigationBar.tintColor = UIColor.black
//        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
//        self.navigationController?.navigationBar.setBackgroundImage(UIColor.white.image(size: CGSize(width: UIScreen.main.bounds.size.width, height: 64)), for: .default)
//        let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_back")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(pop))
//        back.tintColor = .black
//        self.navigationItem.leftBarButtonItem = back
    }
    
    @objc func pop() -> Void {
        
        
        for view in self.navigationController?.navigationBar.subviews ?? [] {
            if view.tag == 10001 {
                if self is NewAnalysisContainerViewController {
                    view.isHidden = true
                }
            }
        }
        
        if self.navigationController != nil {
            self.recovernavigationbar()
            self.navigationController?.popViewController(animated: true)
            return
        }
        
        if let window = UIApplication.shared.delegate?.window {
            
            if let tab = window?.rootViewController as? UITabBarController {
                if let navi = tab.selectedViewController as? UINavigationController {
                    navi.popViewController(animated: true)
                }
            }
        }
    }
    
    func present(vc: UIViewController) -> Void {
        
        self.definesPresentationContext = true
        
        vc.modalPresentationStyle = .overCurrentContext
        
        vc.view.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        
        self.present(vc, animated: false, completion: nil)
    }
    
    func dismiss() -> Void {
        self.dismiss(animated: false, completion: nil)
    }
    
}

